package com.huadi.domain;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "通用返回结果", description = "服务端响应的数据最终都会封装成此对象")
public class ResponseResult<T> implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    @ApiModelProperty(value = "编码")
    private Integer code; //编码
    @ApiModelProperty(value = "错误信息")
    private String msg; //错误信息
    @ApiModelProperty(value = "数据")
    private T data; //数据
    @ApiModelProperty(value = "动态数据")
    private Map map = new HashMap(); //动态数据

    public static <T> ResponseResult<T> success() {
        ResponseResult result = new ResponseResult();
        result.setCode(200);
        result.setMsg("操作成功");
        return result;
    }

    public static <T> ResponseResult<T> success(Integer code, String msg) {
        ResponseResult result = new ResponseResult();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T> ResponseResult<T> error(Integer code, String msg) {
        ResponseResult result = new ResponseResult();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T> ResponseResult<T> success(T object) {
        ResponseResult<T> responseResult = new ResponseResult<T>();
        responseResult.data = object;
        responseResult.code = HttpStatus.OK.value();
        responseResult.msg = "操作成功";
        return responseResult;
    }

    public static <T> ResponseResult<T> success(String msg) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.msg = msg;
        responseResult.code = HttpStatus.OK.value();
        return responseResult;
    }

    public static <T> ResponseResult<T> success(String msg, T object) {
        ResponseResult<T> responseResult = new ResponseResult<T>();
        responseResult.data = object;
        responseResult.code = HttpStatus.OK.value();
        responseResult.msg = msg;
        return responseResult;
    }

    public static <T> ResponseResult<T> error(String msg) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.msg = msg;
        responseResult.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
        return responseResult;
    }

    public ResponseResult<T> add(String key, Object value) {
        this.map.put(key, value);
        return this;
    }
}